<?php

    /* microtimer equivalent - return float */
    function GetTimeAsFloat()
    {
	list($usec, $sec) = explode(" ",microtime());
	return ((float)$usec + (float)$sec);
    }
		
    /* odpala performance counter */
    function StartTimer()
    {
	global $g_szStartTime;
			    
	$g_szStartTime = GetTimeAsFloat();
    }
    
    /* zatrzymuje performance counter - zwraca wynik w sekundach */
    function StopTimer()
    {
	global $g_szStartTime;
			    
	$szTime = round(GetTimeAsFloat()-$g_szStartTime,8);
	return $szTime;
    }
    
    //szybszy odpowiednik time() - uzywamy zamiast time
    function xtime()
    {
    	return $_SERVER['REQUEST_TIME'];
    }
    
    /* simple class dealing with all time routines */
    
    class CTime
    {
		var $m_iCurrTm;
		
		static function &GetInstance()
		{
			global $g_hObjects;
			static $m_hApp = NULL;
						 
			 if(!isset($m_hApp))
			 {
				$m_hApp = new CTime();
				$g_hObjects[] = $m_hApp;
				$m_hApp->OnInit();
			 }
																	 
			 return $m_hApp;
		 }
		
		function OnInit()
		{
			$this->m_iCurrTm = time();    
		}
		
		function OnDestroy()
		{
		}
		
		static function &GetCurrentTime()
		{
			return time();
		}
		
		//set time giving unix timestamp
		function SetUnixTs($iTime)
		{
			$this->m_iCurrTm = $iTime;
		}
		
		//get time as unix timestamp
		function GetUnixTs()
		{
			return $this->m_iCurrTm;
		}
		
		//set time giving iso timestamp
		function SetIsoTs($szTime)
		{
			list($iYear,$iMonth,$iDay,$iHour,$iMin,$iSec) = sscanf($szTime,"%04d-%02d-%02d %02d:%02d:%02d");
			
			$this->m_iCurrTm = mktime($iHour,$iMin,$iSec,$iMonth,$iDay,$iYear);
		}
		
		//get time as iso timestamp
		function GetIsoTs()
		{
			return date("Y-m-d H:i:s",$this->m_iCurrTm);
		}
		
		function GetMonth()
		{
			return date('m',$this->m_iCurrTm);
		}
		
		function GetYear()
		{
			return date('Y',$this->m_iCurrTm);
		}
		
		//make time
		function MakeTime($iYear,$iMonth,$iDay,$iHour,$iMin,$iSec)
		{
			$this->m_iCurrTm = mktime($iHour,$iMin,$iSec,$iMonth,$iDay,$iYear);
		}
		
		//calculate differential time
		function &DiffTime($iFutureTime)
		{
			$aDateTm = array();
		
			$iDiffTime = $iFutureTime - $this->m_iCurrTm;
		
			$aDateTm['days']  = floor($iDiffTime/60/60/24);
			$aDateTm['hours'] = floor(($iDiffTime-$iDaysLeft*60*60*24)/60/60);
			$aDateTm['min']   = floor(($iDiffTime-$iDaysLeft*60*60*24-$iHrsLeft*60*60)/60);
			$aDateTm['sec']   = floor($iDiffTime-$iDaysLeft*60*60*24-$iHrsLeft*60*60-$iMinLeft*60);
			
			return $aDateTm;
		}
		
		//calculate differential time and return it as txt
		function FullDiffTimeAsTxt($iFutureTime)
		{
			$iDiffTime = $iFutureTime - $this->m_iCurrTm;
		
			$iDaysLeft  = floor($iDiffTime/60/60/24);
			$iHrsLeft   = floor(($iDiffTime-$iDaysLeft*60*60*24)/60/60);
			$iMinLeft   = floor(($iDiffTime-$iDaysLeft*60*60*24-$iHrsLeft*60*60)/60);
			$iSecLeft   = floor($iDiffTime-$iDaysLeft*60*60*24-$iHrsLeft*60*60-$iMinLeft*60);
			
			$szTimeTxt = '';
			
			if( $iDaysLeft > 0 )
			{
			$szTimeTxt .= $iDaysLeft.' dni ';
			}
			
			if( $iHrsLeft > 0 )
			{
			$szTimeTxt .= $iHrsLeft.' godz. ';
			}
			
			if( $iMinLeft > 0 )
			{
			$szTimeTxt .= $iMinLeft.' min. ';
			}
			
			if( $iSecLeft > 0 )
			{
			$szTimeTxt .= $iSecLeft.' sek.';
			}
			
			return $szTimeTxt;
		}
		
		function ShortDiffTimeAsTxt($iFutureTime)
		{
			$iDiffTime = $iFutureTime - $this->m_iCurrTm;
		
			$iDaysLeft  = floor($iDiffTime/60/60/24);
			
			if( $iDaysLeft > 0 )
			{
			return $iDaysLeft.' dni ';
			}
			
			$iHrsLeft = floor(($iDiffTime-$iDaysLeft*60*60*24)/60/60);
			
			if( $iHrsLeft > 0 )
			{
			return $iHrsLeft.' godz. ';
			}
			
			$iMinLeft  = floor(($iDiffTime-$iDaysLeft*60*60*24-$iHrsLeft*60*60)/60);
			
			if( $iMinLeft > 0 )
			{
			return $iMinLeft.' min. ';
			}
			
			$iSecLeft   = floor($iDiffTime-$iDaysLeft*60*60*24-$iHrsLeft*60*60-$iMinLeft*60);
			
			if( $iSecLeft > 0 )
			{
			return $iSecLeft.' sek. ';
			}
		}
		
		//add time
		function Add($iDays = 0,$iHrs = 0,$iMin = 0,$iSec = 0)
		{
			$iAddTm = 0;
			
			if($iDays > 0) { $iAddTm += $iDays * 24 * 60 * 60; }
			
			if($iHrs > 0) { $iAddTm += $iHrs * 60 * 60; }
			
			if($iMin > 0) { $iAddTm += $iMin * 60; }
			
			if($iSec > 0) { $iAddTm += $iSec; }
			
			$this->m_iCurrTm += $iAddTm;
			
			return $this->m_iCurrTm;
		}
    }
?>